package com.ibm.cloud.banking.parameter.pricing.service;

import com.ibm.cloud.banking.parameter.pricing.entity.*;
import com.ibm.cloud.banking.parameter.pricing.facade.IPricingCommonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

@Service
public class PricingCommonServiceImpl implements IPricingCommonService {
    static final Logger logger = LoggerFactory.getLogger(PricingCommonServiceImpl.class);
    /**
     * 收费处理计算(定价内部使用)
     *
     * @param pricingChargeParamVo
     *            计收费入参Vo
     * @param pricingAmountProcessParamVo
     *            费用处理入参Vo
     * @return
     */
    @Override
    public ResultVo<FeeItemResultVo> pricingAmountInnerProcess(PricingChargeParamVo pricingChargeParamVo,
                                                               PricingAmountProcessParamVo pricingAmountProcessParamVo) throws BizException {
        String tipMsg = "";
        /**
         * 初始化返回Vo
         */
        ResultVo<FeeItemResultVo> feeItemResultVo = new ResultVo<FeeItemResultVo>();
        ResultVo<PricingChargeResultVo> resultVo = new ResultVo<PricingChargeResultVo>();
        resultVo.setResultCode("00000");
        resultVo.setResultMsg("成功!");
        resultVo.setData(null);
        /**
         * 检验入参
         */
        if (StringUtils.isEmpty(pricingAmountProcessParamVo)) {
            tipMsg = "[收费处理,计算增值税金额失败,入参为空]";
            logger.info(tipMsg);
            throw new BizException("90001", tipMsg);
        }
        /**
         * 3.1. 计算增值税金额
         */
        BigDecimal zzsAmount = new BigDecimal("0"); // 增值税金额
        BigDecimal shAmount = new BigDecimal("0"); // 税后收费金额
        // out.增值税金额=In.收费金额／1.06*0.06
        BigDecimal fee1 = new BigDecimal("1.06");
        BigDecimal fee2 = new BigDecimal("0.06");
        zzsAmount = (pricingAmountProcessParamVo.getFeeAmount().divide(fee1, 2, BigDecimal.ROUND_HALF_UP))
                .multiply(fee2).setScale(2, BigDecimal.ROUND_HALF_UP);
        shAmount = pricingAmountProcessParamVo.getFeeAmount().subtract(zzsAmount).setScale(2, BigDecimal.ROUND_HALF_UP); // 计算out.税后收费金额=In.收费金额-out.增值税金额
        // 设置返回的data数据Vo
        FeeItemResultVo feeItemVo = new FeeItemResultVo();
        feeItemVo.setFeeAmount(pricingAmountProcessParamVo.getFeeAmount());
        feeItemVo.setZzsAmount(zzsAmount);
        feeItemVo.setShAmount(shAmount);
//        /**
//         * 3.2. 调用流水写入方法，进行收费和增值税流水登记
//         */
//        this.recordTransForHsyq(pricingAmountProcessParamVo, feeItemVo, pricingChargeParamVo);
        // 设置返回值
        feeItemResultVo.setData(feeItemVo);
        return feeItemResultVo;
    }
}
